package com.zhang.spark_streaming.wordcount

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
 * @title:
 * @author: zhang
 * @date: 2021/12/12 21:15 
 */
object WordCount {

  def main(args: Array[String]): Unit = {

    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("sparkStreaming")
    val ssc = new StreamingContext(sparkConf,Seconds(3))

    //监听本地端口 9999
    val line: ReceiverInputDStream[String] = ssc.socketTextStream("localhost", 9999)

    val words: DStream[String] = line.flatMap(_.split(" "))
    val wordCount: DStream[(String, Int)] = words.map((_, 1)).reduceByKey(_ + _)
    wordCount.print()
    //启动
    ssc.start()

    ssc.awaitTermination()
  }

}
